package cn.kgc.crowdfundingadmin2.controller;

import cn.kgc.crowdfundingadmin2.pojo.Role;
import cn.kgc.crowdfundingadmin2.service.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class AssignController {

    @Resource
    RoleService roleService;

    @RequestMapping("/assign/to/assign/role/page")
    public String toAssignRolePage(Integer adminId, Integer pageNum, Model model){

        //查询已分配角色
        List<Role> assignedRoleList = roleService.getAssignedRoleList(adminId);
        //查询未分配角色
        List<Role> unAssignedRoleList = roleService.getUnAssignedRoleList(adminId);
        //把数据保存到页面
        model.addAttribute("assignedRoleList",assignedRoleList);
        model.addAttribute("unAssignedRoleList",unAssignedRoleList);
        return "assign-role";
    }

    @RequestMapping("/assign/role")
    public String role(Integer adminId,Integer pageNum,@RequestParam(value = "roleIdList",required = false) List<Integer> roleIdList){
        //执行分配(删除原来的角色，添加新的角色)
        roleService.updateRelationship(adminId,roleIdList);
        //跳转
        return "redirect:/admin/query/for/search?pageNum="+pageNum;
    }

}
